*******************************************************************************
* Balance tests for baseline variables
* for the full sample and the sample that we successfully reinterviewed
* Tables 1 and E.1
*******************************************************************************

use "$data_prep/owwa_data_merged", clear

* List of baseline covariates
local covars_ksa "age married child speak_tagalog max_highschool college worked hsw born_pangasinan born_ncr nointernet pers_sav account contact_dest sal_deduct talk_employer knows_lang mental_health_1 household_size_ dest_Riyadh"
local covars_hk  "age married child speak_tagalog max_highschool college worked hsw born_pangasinan born_ncr nointernet pers_sav account contact_dest sal_deduct talk_employer knows_lang mental_health_1 household_size_"

*******************************************************************************

lab var knows_lang "\shortstack{Knows the name of the language \\ spoken at destination (0/1)}"

*******************************************************************************
* Test randomization for both subgroups

local vars_ksa 20
local vars_hk 19
local vars_talk 20

* Do balance test for both, the full sample and the sample that we successfully reinterviewed
foreach sa in full reint {

	* Data is dropped in the second iteration. Reload the data for subsequent analysis
	if ("`sa'"=="reint") {
		* Drop DWs that never went to HK/KSA
		keep if status_3 < 5 | status_3 >= .
		* Drop if no interview in wave 3
		drop if direct_3==0 & proxy_3==0
	}
	
	* Bocobo (Hong Kong)
	preserve
	keep if ksa==0

	tabstat `covars_hk', statistics(mean) by(gift) columns(statistics) save
	return list
		matrix f_test = r(Stat1)',  r(Stat2)', J(`vars_hk',1,.)
		matrix colnames f_test = Control Treated p-value
		local lcol 0

	local i 1
	foreach var of local covars_hk {
		regress `var' i.gift, r
		matrix f_test[`i',3-`lcol']= Ftail(e(df_m), e(df_r), e(F))
		local i = `i'+1
	}

	count if gift == 0
	local n1 = r(N)
	count if gift == 1
	local n2 = r(N)
	count if gift <= 1
	local n = r(N)

	matrix f_test = f_test \ J(1,3,.) \ [`n1', `n2', .]

	matrix list f_test
	matrix f_test_gift_hk = f_test 

	esttab matrix(f_test, fmt(a2)) using "$outpath_tab/balance_hk_gift_`sa'.tex", replace fragment nogap label nomtitle title(Hong Kong - Gift Treatment)

	restore

	* Pasay (KSA)
	preserve
	keep if ksa==1

	tabstat `covars_ksa', statistics(mean) by(gift) columns(statistics) save
	return list
		matrix f_test = r(Stat1)',  r(Stat2)', J(`vars_ksa',1,.)
		matrix colnames f_test = Control Treated p-value
		local lcol 0

	local i 1
	foreach var of local covars_ksa {
		regress `var' i.gift
		matrix f_test[`i',3-`lcol']= Ftail(e(df_m), e(df_r), e(F))
		local i = `i'+1
	}

	count if gift == 0
	local n1 = r(N)
	count if gift == 1
	local n2 = r(N)
	count if gift <= 1
	local n = r(N)

	matrix f_test = f_test \ [`n1', `n2', .]

	matrix list f_test
	matrix f_test_gift_ksa = f_test
	esttab matrix(f_test, fmt(a2)) using "$outpath_tab/balance_ksa_gift_`sa'.tex", replace fragment nogap label nomtitle title(KSA - Gift Treatment)

	restore
	
	* Output combined tables
	matrix f_test_gift =  f_test_gift_hk, f_test_gift_ksa
	esttab matrix(f_test_gift, fmt(a2)) using "$outpath_tab/balance_gift_`sa'.tex", replace fragment nogap label nolines refcat(age "\midrule" r21 "\midrule", nolabel)

	
	** Adjust tables 
	filefilter "$outpath_tab/balance_gift_`sa'.tex" "$outpath_tab/balance_gift_`sa'_interm.tex", ///
	from("                    & f\BS_test\BS_gift&            &            &            &            &            \BS\BS") ///
	to("  &\BSmulticolumn{3}{c}{Hong Kong}  &\BSmulticolumn{3}{c}{Saudi Arabia}   \BS\BS\BScmidrule(lr){2-4}\BScmidrule(lr){5-7}") replace
	sleep 200
	filefilter "$outpath_tab/balance_gift_`sa'_interm.tex" "$outpath_tab/balance_gift_`sa'_interm2.tex", ///
	from ("r1") to("Destination city is Riyadh (0/1)") replace
	sleep 200
	filefilter "$outpath_tab/balance_gift_`sa'_interm2.tex" "$outpath_tab/balance_gift_`sa'_adj3.tex", ///
	from("r21") to("Observations") replace
	sleep 200
	filefilter "$outpath_tab/balance_gift_`sa'_adj3.tex" "$outpath_tab/balance_gift_`sa'_adj4.tex", ///
	from("\BSmidrule            &            &            &            &            &            &            \BS\BS") to("\BSmidrule") replace
	sleep 200
	filefilter "$outpath_tab/balance_gift_`sa'_adj4.tex" "$outpath_tab/balance_gift_`sa'_adj.tex", ///
	from (" &     Control&     Treated&     p-value&     Control&     Treated&     p-value\BS\BS") ///
	to (" & (1)& (2)& (3)& (4)& (5)& (6)\BS\BS & Control& Treated& p-value& Control& Treated& p-value\BS\BS") replace
	sleep 200

	* Clean up of intermediate tables
	erase "$outpath_tab/balance_gift_`sa'.tex" 
	erase "$outpath_tab/balance_hk_gift_`sa'.tex" 
	erase "$outpath_tab/balance_ksa_gift_`sa'.tex" 
	erase "$outpath_tab/balance_gift_`sa'_interm.tex" 
	erase "$outpath_tab/balance_gift_`sa'_interm2.tex" 
	erase "$outpath_tab/balance_gift_`sa'_adj3.tex" 
	erase "$outpath_tab/balance_gift_`sa'_adj4.tex"
}

copy "$outpath_tab/balance_gift_full_adj.tex" "$outpath_tab/table1.tex", replace
copy "$outpath_tab/balance_gift_reint_adj.tex" "$outpath_tab/table_e1.tex", replace

erase "$outpath_tab/balance_gift_full_adj.tex"
erase "$outpath_tab/balance_gift_reint_adj.tex"